package cn.huiyunche.admin.tms.controller;

import cn.huiyunche.admin.service.DVcstyleOilService;
import cn.huiyunche.base.service.model.DVcstyleOil;
import cn.huiyunche.base.service.vo.DVcstyleOilVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by jxy on 17/1/5.
 */
@Controller
@RequestMapping("/dvcstyleoil")
public class DVcstyleOilController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DVcstyleOilController.class);

    @Autowired
    private DVcstyleOilService dVcstyleOilService;

    /**
     * 查询车型油耗列表
     *
     * @param pageVo
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Result<Object> list(PageVo pageVo, DVcstyleOilVo dVcstyleOilVo) throws BusinessException {
        LOGGER.info("list params PageVo: {},dVcstyleOil:{}", pageVo,dVcstyleOilVo);
        Result<Object> result = new Result<>(true, null, "数据加载成功");
        result.setData(dVcstyleOilService.getListByPage(pageVo,dVcstyleOilVo));
        return result;
    }

    /**
     * 添加车型油耗
     * @param dVcstyleOil 车型油耗实体
     * @return 结果集
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> add(@Valid DVcstyleOil dVcstyleOil, BindingResult br) throws BusinessException {
        if(br.hasErrors()){
            String errorMessage = br.getAllErrors().get(0).getDefaultMessage();
            LOGGER.info("add method has error : "+ errorMessage);
            throw new IllegalArgumentException(errorMessage);
        }
         LOGGER.info("add params dVcstyleOil: {}",dVcstyleOil);
        dVcstyleOilService.addDVcstyleOil(dVcstyleOil);
        Result<Object> result = new Result<>(true,null,"添加车型油耗表数据成功");
        return result;
    }

    /**
     * 修改车型油耗
     * @param dVcstyleOil  车型油耗实体
     * @return 结果集
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> modify(@Valid DVcstyleOil dVcstyleOil, BindingResult br) throws BusinessException {
        if(br.hasErrors()){
            String errorMessage = br.getAllErrors().get(0).getDefaultMessage();
            LOGGER.info("add method has error : "+ errorMessage);
            throw new IllegalArgumentException(errorMessage);
        }
        if(dVcstyleOil.getId() == null || dVcstyleOil.getId() == 0l){
            throw new BusinessException("id 不能为空");
        }
        LOGGER.info("modify params dVcstyleOil: {}",dVcstyleOil);
        dVcstyleOilService.modifyDVcstyleOil(dVcstyleOil);
        Result<Object> result = new Result<>(true,null,"修改车型油耗表数据成功");
        return result;
    }
}
